package cn.gok.util.homework;

import java.util.*;
import java.util.stream.Stream;

class Book{
    private int bid;
    private String bname;
    private float price;

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", bname='" + bname + '\'' +
                ", price=" + price +
                '}';
    }

    public Book(int bid, String bname, float price) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
    }
}
class BookComparable implements Comparator<Book> {
    @Override
    public int compare(Book o1, Book o2) {
        if(o1.getPrice()>o2.getPrice()){
            return -1;
        }else if (o1.getPrice()==o2.getPrice()&&o1.getBname().equals(o2.getBname())){
            return 0;
        }
        return  1;
    }

}
public  class homework6 {
    public static void main(String[] args) {
        作业2();
        作业3();

    }

    private static void 作业3() {
        List<Book> list = new ArrayList<>();
        Collections.addAll(list, new Book(1,"a",45),new Book(2,"b",95),
                new Book(3,"c",42),new Book(4,"d",400),new Book(5,"e",60));
        Stream<Book> stream= list.stream();
        stream.filter(book-> book.getPrice()<150).sorted(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                if (o1.getBid()<o2.getBid()){
                    return -1;
                }else if (o1.getBid()==o2.getBid()&&o1.getBname().equals(o2.getBname()))
                {
                    return 0;
                }
                return 1;
            }
        }).distinct().forEach(System.out::println);
    }

    private static void 作业2() {
        Map<Book,String> map  = new TreeMap<>(new BookComparable());
        map.put(new Book(1,"西游记",40),"老白");
        map.put(new Book(2,"a",10),"老白");
        map.put(new Book(3,"b",20),"老白");
        map.put(new Book(4,"c",20),"老白");
        map.put(new Book(5,"d",50),"老白");
        Iterator<Book> it = map.keySet().stream().iterator();
        while (it.hasNext()){
            Book book = it.next();
            String value = map.get(book);
            System.out.println(book+"="+value);
        }
        Iterator<Map.Entry<Book,String>> it2 = map.entrySet().iterator();
        while (it2.hasNext()){
            Map.Entry<Book,String> e = it2.next();
            Book book = e.getKey();
            String value = e.getValue();
            System.out.println(book+"--"+value);
        }

    }
}